/**
 * Created by wanghaitao on 2018/1/8.
 * getRad 获取圆周
 * getFlatternDistance 计算两点间经纬度距离
 * 参数 lat1, lng1, lat2, lng2 经纬度
 * getAdd gps坐标转百度坐标默认from 1 to 5
 * 参数 lat, long 经纬度
 * getCityName 经纬度转换为城市
 * 参数 lat long 经纬度
 * getDistance 身边店 计算距离
 * 
 */

import Vue from 'vue'
import VueJsonp from 'vue-jsonp'

function getRad(d){
    let PI = Math.PI;
    return d * PI / 180.0;
};

function getFlatternDistance(lat1, lng1, lat2, lng2){
    lat1 = parseFloat(lat1)
    lat2 = parseFloat(lat2)
    lng1 = parseFloat(lng1)
    lng2 = parseFloat(lng2)
    var f = getRad((lat1 + lat2) / 2);
    var g = getRad((lat1 - lat2) / 2);
    var l = getRad((lng1 - lng2) / 2);
    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);
    var s, c, w, r, d, h1, h2;
    var a = 6378137.0;
    var fl = 1 / 298.257;
    sg = sg * sg;
    sl = sl * sl;
    sf = sf * sf;
    s = sg * (1 - sl) + (1 - sf) * sl;
    c = (1 - sg) * (1 - sl) + sf * sl;
    w = Math.atan(Math.sqrt(s / c));
    r = Math.sqrt(s * c) / w;
    if (isNaN(r)) {
        return 0;
    }
    d = 2 * w * a;
    h1 = (3 * r - 1) / 2 / c;
    h2 = (3 * r + 1) / 2 / s;
    return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
};
function getAdd(lat, long) {
    return new Promise(function (resole, reject) {
        Vue.jsonp('https://api.map.baidu.com/geoconv/v1/?coords=' + long + ',' + lat + '&from=3&to=5&ak=ynqZBxotar66E883DYTBx1TNbT6RSAuL&mcode=34:8E:C3:D2:82:1F:11:42:47:64:BB:FF:48:86:F9:D4:63:07:E7:A2;cn.carowl.icfw',
        {}
        ).then(json => {
            alert(json.result);
            // 返回数据 json， 返回的数据就是json格式
            resole(json.result)
        }).catch(err => {
            reject(err)
        })
    })
};
function getCityName(lat, long) {
    return new Promise(function (resole, reject) {
        Vue.jsonp('http://api.map.baidu.com/geocoder/v2/?ak=ynqZBxotar66E883DYTBx1TNbT6RSAuL&callback=renderReverse&location='+lat+','+long+'&output=json&pois=0&mcode=34:8E:C3:D2:82:1F:11:42:47:64:BB:FF:48:86:F9:D4:63:07:E7:A2;cn.carowl.icfw',
        {}
        ).then(resp => {
            //缓存城市名字
            // 返回数据 json， 返回的数据就是json格式
            resole(resp.result)
        }).catch(err => {
            reject(err)
        })
    })
}
function getCityId(cityName) {
    return new Promise(function (resole, reject) {
        Vue.prototype.$.get({
            methodName: 'QueryCityId',
            'cityName': cityName
        }).then(resp => {
            if(resp.data.resultCode === '100'){
                resole(resp.data);
                //缓存城市名字
                Vue.prototype.$.cityId = resp.data.cityId
            }else{
                this.$.toast('服务器错误')
                reject(resp)
            }
        })
    })
}
function getDistance(latitude,longitude,shopList){
    let distanceArr =[];
    for (var i = 0; i < shopList.length; i++) {
        let distance = getFlatternDistance(latitude, longitude, shopList[i].latitude, shopList[i].longitude)
        distance = Math.ceil(distance); // 转换后的距离值
        shopList[i].distance = distance > 1000 ? "距您 " + (distance / 1000).toFixed(1) + "公里" : (distance < 100 ? "距您 < 100m ":" 距您 " + distance + "米") ;
        distanceArr.push(distance)
    }
        for (var j = 0; j < distanceArr.length; j++) {
            for (var k = 0; k < distanceArr.length - 1; k++) {
                if (distanceArr[k] > distanceArr[k + 1]) {
                    var swap = distanceArr[k];
                    distanceArr[k] = distanceArr[k + 1];
                    distanceArr[k + 1] = swap;
                    var swap2 = shopList[k];
                    shopList[k] = shopList[k + 1];
                    shopList[k + 1] = swap2;
                }
            }
    }
    distanceArr.forEach(function (item,index) {
        console.log(item)
        if (item > 5000) {
            shopList.splice(index, distanceArr.length - index)
        }
    })
    return shopList
}
export default {
    getRad,
    getFlatternDistance,
    getAdd,
    getCityName,
    getCityId,
    getDistance
}
